{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# file header"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/dell/anaconda3/envs/CX/lib/python3.8/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.24.3\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n"
     ]
    }
   ],
   "source": [
    "import sys,os,inspect\n",
    "if hasattr(sys.modules[__name__], '__file__'):\n",
    "    _file_name = __file__\n",
    "else:\n",
    "    _file_name = inspect.getfile(inspect.currentframe())\n",
    "CURRENT_FILE_PATH = os.path.dirname(_file_name)\n",
    "sys.path.append(os.getcwd()+\"/../neuronVis\")\n",
    "sys.path.append(os.getcwd()+\"/../resource\")\n",
    "from cx_tool import *\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Insula"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_path = '../resource/project_lateralization/'\n",
    "create_dir(data_path)\n",
    "current_path = data_path+'Insula/'\n",
    "create_dir(current_path)\n",
    "subregion = ['GU','VISC','AI']\n",
    "if not os.path.exists(current_path+'neuron_information.npy'):\n",
    "    type = {}\n",
    "    for region in subregion:\n",
    "        for i in iondata.getNeuronListBySomaRegion(region):\n",
    "            type[i['sampleid']+i['name']] = i['type']\n",
    "    neuron_information = {}\n",
    "    sample_info = {}\n",
    "    for region in subregion:\n",
    "        for i in iondata.getNeuronListBySomaRegion(region):\n",
    "            if len(i['sampleid'])==6 and i['sampleid'][0]!='0' and len(i['name'])==7:\n",
    "                neuron_name = i['sampleid']+i['name']\n",
    "                neuron_information[neuron_name] = {}\n",
    "                control_console_output(0)\n",
    "                neuron_property = iondata.getNeuronPropertyByID(neuron_name[:-7],neuron_name[-7:])\n",
    "                a,b = region_layer_seperate(neuron_property['somaregion'])\n",
    "                neuron_information[neuron_name]['cortex_region'] = cortex_subregion_to_region[neuron_property['somaregion']]\n",
    "                neuron_information[neuron_name]['cortex_subregion'] = a[len(neuron_information[neuron_name]['cortex_region']):]\n",
    "                neuron_information[neuron_name]['soma_position'] = neuron_property['somapoint']\n",
    "                neuron_information[neuron_name]['hemisphere'] = 'left' if neuron_information[neuron_name]['soma_position'][2]<5700 else 'right'\n",
    "                if neuron_name[:-7] not in sample_info:\n",
    "                    sample_info[neuron_name[:-7]] = iondata.getSampleInfo(neuron_name[:-7])[0]['transgenic_line']\n",
    "                neuron_information[neuron_name]['gene'] = sample_info[neuron_name[:-7]]\n",
    "                neuron_information[neuron_name]['type'] = type[neuron_name]\n",
    "                control_console_output(1)\n",
    "    np.save(current_path+'neuron_information.npy',neuron_information)\n",
    "else:\n",
    "    neuron_information = np.load(current_path+'neuron_information.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "color_dict = {'left':color_pool[0],'right':color_pool[3]}\n",
    "tmp = {}\n",
    "for neuron_name in neuron_information:\n",
    "    region = neuron_information[neuron_name]['cortex_region'] + neuron_information[neuron_name]['cortex_subregion']\n",
    "    type = neuron_information[neuron_name]['type']\n",
    "    hemishphere = neuron_information[neuron_name]['hemisphere']\n",
    "    gene = neuron_information[neuron_name]['gene'].replace('/','_')\n",
    "    if region not in tmp:\n",
    "        tmp[region] = {}\n",
    "    if type not in tmp[region]:\n",
    "        tmp[region][type] = {}\n",
    "    if hemishphere not in tmp[region][type]:\n",
    "        tmp[region][type][hemishphere] = {}\n",
    "    if gene not in tmp[region][type][hemishphere]:\n",
    "        tmp[region][type][hemishphere][gene] = []\n",
    "    tmp[region][type][hemishphere][gene].append(neuron_name)\n",
    "for i in tmp:\n",
    "    for j in tmp[i]:\n",
    "        for k in tmp[i][j]:\n",
    "            for l in tmp[i][j][k]:\n",
    "                save_scene(tmp[i][j][k][l],color_dict[k],current_path+'_'.join([l,i,j,k]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('CX')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "6684bfb8f990107dc2e41da7bb6829210ca36b987584a9d9d8b153337675e487"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
